var argv = require('yargs').argv
var exec= require("child_process").exec;
var util = require('util');
var fs = require('fs');
var path = require('path');
var process = require('process');
var config = require('./config');

var PAGE = config.build.page;
 
exec = util.promisify(exec);

var execpath = `src/${PAGE}/ts`;

var exexstr = `tsc --p src/${PAGE}/ts`;
if (argv.watch) {
    exexstr += " --watch";
}

async function build() {
   try {
        var { stdout, stderr } = await exec(exexstr);
        console.log('stdout:', stdout);
        console.log('stderr:', stderr);
   } catch(e) {
        console.log(e);
   }
}

if (fs.existsSync(path.join(process.cwd(), execpath))) {
    build();
}